import javax.jms.*;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.QueueViewMBean;

public class Producer {
	
	private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
	private static String subject = "First message";

	private static void useJSM() throws JMSException {

		ActiveMQConnectionFactory activeMQConnectionFactory =
				new ActiveMQConnectionFactory(url);
		Connection connection = activeMQConnectionFactory.createConnection();
		connection.start();
		Session session = connection.createSession(false,
	            Session.AUTO_ACKNOWLEDGE);
		
		Destination destination = session.createQueue(subject);

        MessageProducer messageProducer;
        
        for (int i = 0; i < 3; ++i) {
	        messageProducer = session.createProducer(destination);
	
	        TextMessage message = session.createTextMessage("Hello world!" + i);
	        messageProducer.send(message);
	        System.out.println("Sent message '" + message.getText() + "'");
        }

        connection.close();

	}
	
	private static void useJMX() throws Exception {
		

//		String operationName="addQueue";
//		String parameter="MyNewQueue";
		
//		if(parameter != null) {
//			Object[] params = {parameter};
//			String[] sig = {"java.lang.String"};
//			mBeanServerConnection.invoke(activeMQ, operationName, params, sig);
//		} else {
//			mBeanServerConnection.invoke(activeMQ, operationName,null,null);
//		}
		
		QueueViewMBean queueViewMBean = JMXServices.getQueueViewMBean("First message");
		for (int i = 0; i < 2; ++i) {
			queueViewMBean.sendTextMessage("Now it works!" + i);
		}

	}
	
	public static void main(String[] args) throws Exception {
		useJMX();
		//useJSM();
	}

}
